home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / BSINIT.C < prev    next >
C/C++ Source or Header  |  1995-09-14  |  9KB  |  295 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    bsinit.c
  5. //   Title:    Base library
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //
  24. //    This module contains code to initialize the base library.
  25. //    UNIX and MSC should always call these routines.
  26. //    This routine registers an 'atexit' routine to do clean-up. This routine
  27. //    should be called if the application exits abnormally (execl,...).
  28. //
  29. //    This module also computes executable load size under DOS and writes
  30. //    it to the file 'progsize.log'.
  31. //
  32. //    The size of a program cannot be determined statically because the
  33. //    the  stack and near heap are sized dynamically at run time.  The
  34. //    load size  specified in the EXE header is incorrect for this
  35. //    reason.  The only way  to accurately determine the program image
  36. //    size is to check it at run time.
  37. //
  38. //    This program will get the current size of the program image in
  39. //    memory. This size will change dynamically as the program acquires
  40. //    memory from and releases memory to DOS.  This in formation is
  41. //    stored in the Memory Control Block (MCB).  The MCB begins one
  42. //    paragraph (16 bytes) before the Program Segment Prefix (PSP).
  43. //    The current program size is stored at offset 0x3 from the
  44. //    beginning of the MCB.  Offset 0x3 represents the size in
  45. //    paragraphs therefore it must be multiplied by 16 to obtain the
  46. //    size in bytes.
  47. //
  48. //    Environment variables
  49. //        __DEBUGGER__        Disable keyboard monitor and other interrupts
  50. //                                while debugging
  51. //        LOG_LEVEL            Set current logging level
  52. //        NO_KBD_MONITOR        Disable keyboard monitor (Ctrl-C/Ctrl-Alt-Del)
  53. //
  54. //    The code in this module should be written entirely in C. 
  55. //    Do not use any C++ constructs.
  56. //
  57. //    This module is portable to:
  58. //        DOS 3.X+
  59. //        MS Windows 3.X+
  60. //        OS/2 2.X+
  61. //        OS/2 2.0 PM
  62. //        SCO UNIX.
  63. //
  64. //    The following compilers are supported:
  65. //        MSC 6.0A
  66. //        MSC/C++ 7.0
  67. //        Borland C++ 3.1 for DOS
  68. //        Borland C++ 1.0 for OS/2 2.X
  69. //        SCO UNIX cc
  70. //
  71. //----------------------------------------------------------------------------
  72. #include <bs.h>
  73.  
  74.  
  75. //----------------------------------------------------------------------------
  76. //    Globals
  77. //----------------------------------------------------------------------------
  78. static BOOL fInit = FALSE;
  79. static void BaseLibraryTerminate_Internal(void);
  80.  
  81. #if OS_WINDOWS                                    // Instance handle for current window
  82. static HANDLE hInstance = 0;                // application
  83. static HWND hwndMain = 0;
  84. #endif
  85.  
  86. #define MAX_EXIT_FUNCS        (32)
  87.  
  88. static SIZET cExitFuncs = 0;
  89. static PFNEXIT apfnexit[MAX_EXIT_FUNCS];
  90. static SIZET acExitPriority[MAX_EXIT_FUNCS];
  91.  
  92.  
  93. //----------------------------------------------------------------------------
  94. //   Description:    Register an exit function with the base library.
  95. //                          This function is similar to the C library 'atexit' but
  96. //                        is duplicated here so there is more control.
  97. //    Parameters:
  98. //
  99. //                        Higher priority functions are called last.
  100. //                        Base library uses 128+
  101. //       Returns:    
  102. //----------------------------------------------------------------------------
  103. BOOL FN_E BaseExitFunc(PFNEXIT pfnexit, SIZET cPriority)
  104. {
  105.     SIZET i;
  106.  
  107.     Assert(pfnexit);
  108.  
  109.     for (i = 0; i < cExitFuncs; ++i)        // Check duplicate functions
  110.         if (apfnexit[i] == pfnexit)
  111.             return TRUE;
  112.  
  113.     if (cExitFuncs >= MAX_EXIT_FUNCS)
  114.         return Error("Maximum number of exit functions exceeded.");
  115.  
  116.     apfnexit[cExitFuncs] = pfnexit;
  117.     acExitPriority[cExitFuncs] = cPriority;
  118.     cExitFuncs++;
  119.     return TRUE;
  120. }
  121.  
  122.  
  123. //----------------------------------------------------------------------------
  124. //   Description:    Base library initialize
  125. //    Parameters:    argc, argv        Parameters to main()
  126. //                        hinst                Windows startup parameters
  127. //                        hPrevInst
  128. //                        lpCmdLine
  129. //                        nCmdShow
  130. //       Returns:    void. This routines aborts to DOS if it fails during 
  131. //                        initialization.
  132. //----------------------------------------------------------------------------
  133. #if OS_WINDOWS
  134. VOID FN_E BaseLibraryInitialize(HINSTANCE hinst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow, PBS_CFG pcfg)
  135. #else
  136. VOID FN_E BaseLibraryInitialize(int argc, char **argv, PBS_CFG pcfg)
  137. #endif
  138. {
  139.     if (fInit)
  140.         return ;
  141.  
  142.     fInit = TRUE;
  143.     cExitFuncs = 0;                            // Reset exit function count
  144.  
  145. #if OS_WINDOWS                                    // Instance handle for current window
  146.     hInstance = hinst;                        // Store instance globally
  147. #endif
  148.  
  149. #if OS_WINDOWS==0 || COMPILE_DLL==0
  150.     atexit(BaseLibraryTerminate_Internal);
  151. #endif
  152.  
  153.     OutputInitialize();
  154. //    ClearStack();
  155.     HeapInitialize();
  156.     BaseExitFunc((PFNEXIT)FileTerminate, SYS_EXIT_PRIORITY);
  157.  
  158. #if OS_DOS
  159.     UmbSetState(1);
  160. #endif
  161.  
  162.     ProgramSizeShow("at start up");
  163.  
  164. #if OS_DOS
  165.     SleepInitialize();
  166. #endif
  167.  
  168. #if OS_DOS || OS_OS2
  169.     KbdInitialize();
  170. #endif
  171.  
  172. #if OS_WINDOWS
  173.     CommandLineInitialize(hinst, hPrevInst, lpCmdLine, nCmdShow);
  174. #else
  175.     CommandLineInitialize(argc, argv);
  176. #endif
  177.  
  178. #if OS_DOS && COMPILER_BORLAND
  179.     if (!DebugMode())
  180.         {
  181. //     CtrlCInitialize();                // Commented out to support Zinc!
  182.         CritErrInitialize();
  183.         }
  184. #endif
  185.  
  186.     LogOpen();
  187.     LogSetLevel((USHORT)EnvVal("LOG_LEVEL"));
  188.  
  189.     Log(LOG_VERY_HIGH, "%s initialized.", CommandLineAppName());
  190.  
  191.     if (pcfg)                                    // Set default configuration
  192.         CfgSet(NULL, CFG_ALL, pcfg);
  193.  
  194.     return ;
  195. }
  196.  
  197. //----------------------------------------------------------------------------
  198. //   Description:    Base library terminate.
  199. //    Parameters:    
  200. //       Returns:    
  201. //----------------------------------------------------------------------------
  202. VOID FN_E BaseLibraryTerminate(void)
  203. {
  204.     BaseLibraryTerminate_Internal();
  205.     return ;
  206. }
  207.  
  208.  
  209. //----------------------------------------------------------------------------
  210. //   Description:    Base library terminate. Internal -- called by atexit!
  211. //                          NOTE: Assert() and Halt() exit directly to the OS using 
  212. //                                _exit(). Be sure to modify those routine to restore
  213. //                                any modified interrupt handlers!
  214. //    Parameters:    
  215. //       Returns:
  216. //----------------------------------------------------------------------------
  217. static void BaseLibraryTerminate_Internal(void)
  218. {
  219.     BOOL fDone;
  220.     SIZET i, cPriority, cNext;
  221.  
  222.     if (!fInit)
  223.         return;
  224.                                                     
  225.     ProgramSizeShow("at termination");    // Show terminal size
  226.     Log(LOG_VERY_HIGH, "%s terminating.", CommandLineAppName());
  227.  
  228.     fInit = FALSE;
  229.  
  230.     fDone = FALSE;
  231.     cPriority = 0;
  232.     while (!fDone)                                // Call exit functions in the proper order
  233.         {
  234.         cNext = MAX_SIZET;
  235.         fDone = TRUE;
  236.  
  237.         for (i = cExitFuncs; i; --i)        // Scan from last to first
  238.             {                                        // Watch for next exit level!
  239.             if (acExitPriority[i-1] > cPriority && acExitPriority[i-1] < cNext)
  240.                 {
  241.                 cNext = acExitPriority[i-1];
  242.                 fDone = FALSE;
  243.                 }                                    // Call exit functions
  244.             if (acExitPriority[i-1] == cPriority)
  245.                 (*apfnexit[i-1])();
  246.             }
  247.         cPriority = cNext;
  248.         }
  249.     return ;
  250. }
  251.  
  252.  
  253. //----------------------------------------------------------------------------
  254. //   Description:    
  255. //    Parameters:
  256. //       Returns:    Program instance handle
  257. //----------------------------------------------------------------------------
  258. #if OS_WINDOWS                                    
  259. HINSTANCE FN_E WindowsInstance(void)
  260. {
  261.     return hInstance;
  262. }
  263. #endif
  264.  
  265.  
  266. //----------------------------------------------------------------------------
  267. //   Description:    
  268. //    Parameters:
  269. //       Returns:    Program instance handle
  270. //----------------------------------------------------------------------------
  271. #if OS_WINDOWS                                    
  272. HWND FN_E WindowsMainWindow(void)
  273. {
  274.     return hwndMain;
  275. }
  276. #endif
  277.  
  278.  
  279. //----------------------------------------------------------------------------
  280. //   Description:    
  281. //    Parameters:
  282. //       Returns:    Program instance handle
  283. //----------------------------------------------------------------------------
  284. #if OS_WINDOWS                                    
  285. VOID FN_E WindowsSetMainWindow(HWND hwnd)
  286. {
  287.     Assert(hwnd);
  288.     hwndMain = hwnd;
  289.     return ;
  290. }
  291. #endif
  292. //----------------------------------------------------------------------------
  293. //------------------------------- End of File --------------------------------
  294. //----------------------------------------------------------------------------
  295.